package com.ai.dtcore.service.impl;

import java.util.Map;

import com.ai.dtcore.service.ICacheService;
import com.ai.dtcore.util.JedisUtil;
import com.ai.dtcore.util.MyOwnRuntimeException;
import com.ai.frame.util.JsonUtil;

import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.Protocol;

public class CacheServiceImpl implements ICacheService {
    // 调用成功标识
    private static final String IS_OK = Protocol.Keyword.OK.name();

    @Override
    public boolean put2Cache(String cacheKey, String value)  throws MyOwnRuntimeException {
        JedisCluster cluster;
		try {
			cluster = JedisUtil.getCluster();
		} catch (Exception e) {
			throw new MyOwnRuntimeException(e);
		}
        String result = cluster.set(cacheKey, value);
//        cluster.close();
        return IS_OK.equals(result);
    }

    @Override
    public boolean put2Cache(String cacheKey, String value, int seconds)  throws MyOwnRuntimeException {
        JedisCluster cluster;
		try {
			cluster = JedisUtil.getCluster();
		} catch (Exception e) {
			throw new MyOwnRuntimeException(e);
		}
        String result = cluster.setex(cacheKey, seconds, value);
//        cluster.close();
        return IS_OK.equals(result);
    }

    @Override
    public String getFromCache(String cacheKey) throws MyOwnRuntimeException  {
        JedisCluster cluster;
		try {
			cluster = JedisUtil.getCluster();
		} catch (Exception e) {
			throw new MyOwnRuntimeException(e);
		}
        String str = cluster.get(cacheKey);
//        cluster.close();
        return str;
    }

    @Override
    public long incr(String key) throws MyOwnRuntimeException {
        JedisCluster cluster;
		try {
			cluster = JedisUtil.getCluster();
		} catch (Exception e) {
			throw new MyOwnRuntimeException(e);
		}
        Long longValue = cluster.incr(key);
        if (longValue == 900) {
			cluster.expire(key, 1);
		}
//        cluster.close();
        return longValue;
    }

    @Override
    public long decr(String key) throws MyOwnRuntimeException {
        JedisCluster cluster;
		try {
			cluster = JedisUtil.getCluster();
		} catch (Exception e) {
			throw new MyOwnRuntimeException(e);
		}
        Long longValue = cluster.decr(key);
//        cluster.close();
        return longValue;
    }

    @Override
    public String putMap(String key, Map<String, String> map) throws MyOwnRuntimeException {
        System.out.println("putMap  key="+key +"   value="+JsonUtil.convertObject2Json(map));
        JedisCluster cluster;
		try {
			cluster = JedisUtil.getCluster();
		} catch (Exception e) {
			throw new MyOwnRuntimeException(e);
		}
        String str = cluster.hmset(key, map);
//        cluster.close();
        return str;
    }

    @Override
    public Map<String, String> getMap(String key) throws MyOwnRuntimeException {
        JedisCluster cluster;
		try {
			cluster = JedisUtil.getCluster();
		} catch (Exception e) {
			throw new MyOwnRuntimeException(e);
		}
        Map<String, String> map = cluster.hgetAll(key);
//        cluster.close();
        return map;
    }

    @Override
    public Long lpush(String key, String... values) throws MyOwnRuntimeException {
        JedisCluster cluster;
		try {
			cluster = JedisUtil.getCluster();
		} catch (Exception e) {
			throw new MyOwnRuntimeException(e);
		}
        Long longValue = cluster.lpush(key, values);
//        cluster.close();
        return longValue;
    }

    public Long rpush(String key, String... values) throws MyOwnRuntimeException {
        JedisCluster cluster;
		try {
			cluster = JedisUtil.getCluster();
		} catch (Exception e) {
			throw new MyOwnRuntimeException(e);
		}
        Long longValue = cluster.rpush(key, values);
//        cluster.close();
        return longValue;
    }

    @Override
    public String lpop(String key) throws MyOwnRuntimeException {
        JedisCluster cluster;
		try {
			cluster = JedisUtil.getCluster();
		} catch (Exception e) {
			throw new MyOwnRuntimeException(e);
		}
        String str = cluster.lpop(key);
//        cluster.close();
        return str;
    }

    @Override
    public String rpop(String key) throws MyOwnRuntimeException {
        JedisCluster cluster;
		try {
			cluster = JedisUtil.getCluster();
		} catch (Exception e) {
			throw new MyOwnRuntimeException(e);
		}
        String str = cluster.rpop(key);
//        cluster.close();
        return str;
    }
}